package com.sen.app.schedul.services.impl;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.util.Assert;

import com.sen.app.schedul.core.utils.DirUtils;
import com.sen.app.schedul.entity.JobConfigEntity;
import com.sen.app.schedul.services.SchedulManagerService;
import com.thoughtworks.xstream.XStream;

/**
 * scheduler管理service接口具体实现
 * 
 * @author sen
 */
public class SchedulManagerServiceImpl implements SchedulManagerService {

    /**
     * add targger, 新增到默认组
     * 
     * @param triggerName
     * @param jobName
     * @param cronExpression
     * @param scheduler 被操作的scheduler类
     * @throws SchedulerException
     */
    public void addTrigger(String triggerName, String jobName, String cronExpression, Scheduler scheduler)
    throws SchedulerException {
        addTrigger(triggerName, Scheduler.DEFAULT_GROUP, jobName,
                Scheduler.DEFAULT_GROUP, cronExpression, scheduler);
    }

    /**
     * add targger, 新增到triggerGroupName
     * 
     * @param triggerName
     * @param triggerGroupName
     * @param jobName
     * @param jobGrourpName
     * @param cronExpression
     * @param scheduler 被操作的scheduler类
     * @throws SchedulerException
     */
    public void addTrigger(String triggerName, String triggerGroupName, String jobName, String jobGrourpName, 
            String cronExpression, Scheduler scheduler) throws SchedulerException {
        Assert.isTrue(StringUtils.isBlank(jobName), "新增targger出错,原因:jobName不能为空!");
        Assert.isTrue(StringUtils.isBlank(triggerName), "新增targger出错,原因:triggerName不能为空!");
        Assert.isNull(scheduler, "新增targger出错,原因:scheduler不能为空!");

        JobDetail jobDetail = scheduler.getJobDetail(jobName, jobGrourpName);
        if (jobDetail != null) {
            scheduler.addJob(jobDetail, true);
            
        } else {
            
        }

    }
    
    /**
     * 获取jobs目录下所有job配置
     * @return Map, key为文件名
     */
    public Map<String, JobConfigEntity> getAllJobConfig() {
        Map<String, JobConfigEntity> retVal = new HashMap<String, JobConfigEntity>();
        
        String scanPath = DirUtils.getJobsDir();
        if (StringUtils.isBlank(scanPath)) {
            return retVal;
        }
        File scanDir = new File(scanPath);
        if (!scanDir.exists() || !scanDir.isDirectory()) {
            return retVal;
        }
        
        String[] type = {"xml"};
        Collection<File> files = FileUtils.listFiles(scanDir, type, false);
        if (CollectionUtils.isNotEmpty(files)) {
            Iterator<File> iterator = null;
            for (iterator = files.iterator(); iterator.hasNext();) {
                File file = iterator.next();
                try {
                    XStream xStream = new XStream();
                    xStream.alias("jobConfig", JobConfigEntity.class);
                    JobConfigEntity bean = (JobConfigEntity) xStream.fromXML(file);
                    bean.setModifyTime(file.lastModified());
                    retVal.put(file.getPath(), bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return retVal;
    }
    
    public static void main(String[] args) {
        String jobdir = DirUtils.getJobsDir();
        System.out.println(FileUtils.isFileNewer(new File(jobdir), new Date().getTime()));
        SchedulManagerServiceImpl impl = new SchedulManagerServiceImpl();
        Map<String, JobConfigEntity> datas = impl.getAllJobConfig();
        System.out.println(datas);
    }
}
